<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>AUI快速完成布局</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />


</head>

<body>
      <img src="" alt="" id="img"  onclick="api.closeWin()" style="width:100%" >
      <footer class="aui-bar aui-bar-tab" id="footer" style="background:#ff4285;color:#fff;line-height:55px;" onclick="api.closeWin()">
          <div>关闭<div>
      </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="./conf/config.js"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
    apiready = function() {
        initEventListenner(); //退出键添加提示框
        $("#img").attr("src",api.pageParam.src);

    };

    function longpress(el, fn_longpress, fn_click, longpressTime) {
        longpressTime = longpressTime || 800;
        time_longpress = setTimeout(function() {
            time_longpress = 0;
            eval(fn_longpress);
        }, longpressTime);
        el.ontouchmove = function() {
            clearTimeout(time_longpress);
            time_longpress = 0;
        }
        el.ontouchend = function() {
            clearTimeout(time_longpress);
            if (time_longpress != 0) {
                eval(fn_click);
            }
        }
    }

    function fnlong() {

    // var canvas = $('#qrcode').find("canvas").get(0);
    //
    //     try {//解决IE转base64时缓存不足，canvas转blob下载
    //     var blob = canvas.msToBlob();
    //     navigator.msSaveBlob(blob, 'qrcode.jpg');
    // } catch (e) {//如果为其他浏览器，使用base64转码下载
    //     var url = canvas.toDataURL('image/jpeg');
    //     //$("#download").attr('href', url).get(0).click();
    // }
        api.actionSheet({
            title: '保存相片到相册',

            buttons: ['保存']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {

                fnSaveImg();
            }
        });
    }

    function fnclick(el) {
        api.toast({
            msg: '长按保存',
            duration: 2000,
            location: 'middle'
        });
    }

    //长按下载保存
    function fnSaveImg() {
      var src2=$("#img").attr("src");
       //var timestamp = Date.parse(new Date());
       var timestamp=RandomNumBoth(100000,999999);
        api.showProgress({
            title: '努力保存中...',
            text: '先喝杯茶...',
            modal: false
        });
      //alert(url);
        api.download({
            url:src2,
            savePath: 'fs://iamge/'+timestamp+'.jpg'
        }, function(ret, err) {
            if (ret) {
                if (ret.state == 1) {
                    api.saveMediaToAlbum({
                        path: 'fs://iamge/'+timestamp+'.jpg'
                    }, function(ret, err) {
                        if (ret && ret.status) {
                            //alert(JSON.stringify(ret));
                            api.hideProgress();
                            api.confirm({
                                title: '查看图片',
                                msg: '图片保存成功',
                                buttons: ['现在查看', '取消']
                            }, function(ret, err) {
                                var index = ret.buttonIndex;
                                if (index == 1) {
                                    api.getPicture({
                                        sourceType: 'album',
                                        encodingType: 'jpg',
                                        mediaValue: 'pic',
                                        destinationType: 'url',
                                        allowEdit: true,
                                        quality: 50,
                                        targetWidth: 100,
                                        targetHeight: 100,
                                        saveToPhotoAlbum: false
                                    }, function(ret, err) {
                                        //chakan不做处理
                                    });
                                }
                            });
                        } else {
                            alert('保存失败');
                        }
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });


    }
    function RandomNumBoth(Min,Max){

          var Range = Max - Min;

          var Rand = Math.random();

          var num = Min + Math.round(Rand * Range); //四舍五入

          return num;

    }
    function initEventListenner() {
        api.addEventListener({
            name: 'keyback'
        }, function(ret, err) {
            api.closeWin();
        });
    };
</script>

</html>
